package com.hzy.elastic.service;

import com.hzy.elastic.entity.QStudent;
import com.hzy.elastic.entity.Student;
import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.stereotype.Repository;

@Repository
public interface MyRepository extends QuerydslPredicateExecutor<Student>, QuerydslBinderCustomizer<QStudent> {
    /**
     * Student查询入口统一自定义过滤
     */
    @Override
    default void customize(QuerydslBindings bindings, QStudent student) {
        /**
         * 通过name匹配name或nickname
         */
        bindings.bind(student.name).first((path, value) ->
                student.name.contains(value).or(student.name.contains(value))
        );

        /**
         * 查询字段黑名单
         * 如：禁止通过性别查询
         */
        bindings.excluding(student.age);

        /**
         * 忽略大小写
         */
        bindings.bind(String.class).first((StringPath path, String value) -> path.containsIgnoreCase(value));
    }
}
